package com.bdweb.controller;

import com.bdweb.model.POWER;
import com.bdweb.pojo.Result;

import com.bdweb.service.PowerAllService;
import com.bdweb.service.PowerService;
import com.bdweb.service.impl.MySqlUpdater;



import   com.bdweb. mokuai.UDPClient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class powerController {
    @Autowired
    private PowerService powerService;
    @Autowired
    PowerAllService powerAllService;

    // 获取电箱数据
    @CrossOrigin(origins = "*")   //设置跨域
    @GetMapping("/powerList")
    public Result getpowerList() {
        List<POWER> allPower = powerService.findAllPower();

        return new Result(200, "success", allPower);
    }


    //电箱开启关闭
    @Autowired
    private MySqlUpdater mySqlUpdater;

    @CrossOrigin(origins = "*")   //设置跨域

    @GetMapping("/powerswitch")
    public Result getPowerOpen(String id) throws IOException, InterruptedException {


        List<POWER> allPower = powerService.findAllPower();
     //   new UDPClient1("AT+STACH0=0\r\n","192.168.1.43",6800);
      //  new UDPClient1("AT+STACH0=1\r\n","192.168.1.43",6800);
        int newId = Integer.parseInt(id);
        System.out.println(newId);
        if(newId<10) {
            for (int i = 0; i < allPower.size(); i++) {
                if (newId==i){
                    System.out.println(allPower.get(newId).getCodeopen()+"\r\n"+"333333");
                   // System.out.println(allPower.get(newId).getCodeopen()+"333333");
                    System.out.println(allPower.get(newId).getIp()+"333333");
                    // 开启单个电箱
                    new UDPClient1(allPower.get(newId).getCodeopen()+"\r\n", allPower.get(newId).getIp(), Integer.parseInt(allPower.get(newId).getPort()));
                    // 修改bd_power数据库数据
                    mySqlUpdater.updateValue("bd_power",newId + 1, "isopen", "1");
                }
            }
        }if(newId>=10&&newId<25){
            newId=newId-allPower.size();

            // 开启单个电箱
            new UDPClient1(allPower.get(newId).getCodeclose()+"\r\n", allPower.get(newId).getIp(), Integer.parseInt(allPower.get(newId).getPort()));
            // 修改bd_power数据库数据
            mySqlUpdater.updateValue("bd_power",newId + 1, "isopen", "0");
        }



        /*// 把字符串转换为int类型
        //控制码开
        System.out.println(id);

        if (newId >= 0 && newId <= 10) {




            // 修改bd_power数据库数据
            mySqlUpdater.updateValue("bd_power",newId + 1, "isopen", "1");



            // 开启单个电箱
            new UDPClient1(allPower.get(newId).getCodeopen(), allPower.get(newId).getIp(), Integer.parseInt(allPower.get(newId).getPort()));

        }

        if (newId > 10 ||newId <30) {
          newId=newId-10;
            // 关闭单个电箱
            new UDPClient1(allPower.get(newId - allPower.size()).getCodeclose(), allPower.get(newId - allPower.size()).getIp(), Integer.parseInt(allPower.get(newId - allPower.size()).getPort()));
          // 修改bd_power数据库数据
            mySqlUpdater.updateValue("bd_power",newId - (allPower.size()+1), "isopen", "0");


        }*/
        if(newId==31){
            //所有电箱全开

           powerAllService.listPowerOpenAll();
            mySqlUpdater.updateValueAllOpen("bd_power","isopen",1);
        }


        if(newId==32){
            //所有电箱全关

            powerAllService.listPowerCloseAll();
            mySqlUpdater.updateValueAllOpen("bd_power","isopen",0);
           /* for (int i = 0; i < allPower.size(); i++) {
                new UDPClient1(allPower.get(i).getCodeclose(), allPower.get(i).getIp(), Integer.parseInt(allPower.get(i).getPort()));
            }
           */
        }


        return new Result(200, "success", "OK");
    }

}
